﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Configuration;

namespace envioMails
{
    public class Envio
    {
        public static void mandarMail(string mailto, string mailSubject, string mailBody)
        {
            MailMessage correo = new MailMessage();
            SmtpClient smtp = new SmtpClient();
            foreach (string item in mailto.Split(';'))
            {
                correo.To.Add(item);
            }
            string subject = mailSubject;
            string smtpHost = ConfigurationManager.AppSettings["smtpHost"].ToString();
            string smtpUsername = ConfigurationManager.AppSettings["smtpUsername"].ToString();
            string smtpPassword = ConfigurationManager.AppSettings["smtpPassword"].ToString();
            string correoFrom = ConfigurationManager.AppSettings["smtpFrom"].ToString();
            correo.From = new MailAddress(correoFrom);
            correo.Subject = subject;
            correo.Body = mailBody;
            correo.IsBodyHtml = true;
            correo.Priority = MailPriority.Normal;
            smtp.Host = smtpHost;
            smtp.Port = 587;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential(smtpUsername, smtpPassword);
            smtp.EnableSsl = true;
            try
            {
                smtp.Send(correo);
            }
            catch (Exception ex)
            {
                throw new Exception("Error al enviar el mail. " + ex.InnerException.ToString());
            }
        }
    }
}
